﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Reflection;
using System.ComponentModel;

namespace PUBCOM
{
    public class Input
    {

        public static string GetEnumDesc(Enum e)
        {
            FieldInfo EnumInfo = e.GetType().GetField(e.ToString());
            DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])EnumInfo.
                GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (EnumAttributes.Length > 0)
            {
                return EnumAttributes[0].Description;
            }
            return e.ToString();
        }


        /// <summary>
        /// 判断是否有值，否则返回默认值
        /// </summary>
        /// <param name="value"></param>
        /// <param name="DefaultValue"></param>
        /// <returns></returns>
        static public string GetDefault(object value, string DefaultValue)
        {
            if (null != value)
                if (!string.IsNullOrEmpty(value.ToString()))
                    return value.ToString();
            return DefaultValue;
        }

        #region 获取参数
        /// <summary>
        /// 返回参数值
        /// </summary>
        /// <param name="param">参数名</param>
        /// <returns></returns>
        static public string GetRequstParam(string param)
        {
            return GetRequstParam(param, 1, "");
        }

        /// <summary>
        /// 返回参数值
        /// </summary>
        /// <param name="param">参数名</param>
        /// <param name="mode">获取方式1：QueryString 2:Form 3:全部Params</param>
        /// <returns></returns>
        static public string GetRequstParam(string param, int mode)
        {
            return GetRequstParam(param, mode, "");
        }

        /// <summary>
        /// 返回参数值
        /// </summary>
        /// <param name="param">参数名</param>
        /// <param name="mode">获取方式1：QueryString 2:Form 3:全部Params</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        static public string GetRequstParam(string param, int mode, string defaultValue)
        {
            string Value = string.Empty;
            switch (mode)
            {
                case 1:
                    Value = HttpContext.Current.Request.QueryString[param];
                    break;
                case 2:
                    Value = HttpContext.Current.Request.Form[param];
                    break;
                default:
                    Value = HttpContext.Current.Request.Params[param];
                    break;

            }
            return GetDefault(Filter(Value), defaultValue);
        }

        #endregion

        #region 设置URL参数
        /// <summary>
        /// 设置URL参数(如果存在则替换，否则添加参数)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="section"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        static public string seturl(string url, string section, object value)
        {
            if (section == null) return ("seturl中第二个参数不得为空");
            if (value == null) value = "";
            if (url == null) url = HttpContext.Current.Request.Url.ToString();
            var rval = url;
            var sec1 = "?" + section + "=";
            var sec2 = "&" + section + "=";
            var parai = url.IndexOf(sec1);
            var have = true;
            if (parai == -1)
            {
                parai = url.IndexOf(sec2);
                if (parai == -1)
                {
                    have = false;
                }
            }
            if (have)//这个section已经存在了,需要替换
            {
                var paras = url.Substring(parai + section.Length + 2);
                var para2i = paras.IndexOf("&");
                if (para2i == -1)//后面没有其它参数了
                {
                    rval = url.Substring(0, parai + section.Length + 2) + value;
                }
                else//后面还有其它参数
                {
                    rval = url.Substring(0, parai + section.Length + 2) + value + paras.Substring(para2i);
                }
            }
            else//这个section不存在,需要附加
            {
                if (url.IndexOf("?") == -1)
                {
                    rval = url + "?" + section + "=" + value;
                }
                else
                {
                    rval = url + "&" + section + "=" + value;
                }
            }
            return rval;
        }

        #endregion


        #region 字符串过滤
        /// <summary>
        /// 过滤字符
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public static string Filter(string sInput)
        {
            if (sInput == null || sInput == "")
                return null;
            string sInput1 = sInput.ToLower();
            string output = sInput;
            string pattern = @"*|and|exec|insert|select|delete|update|count|master|truncate|declare|char(|mid(|chr(|'";
            if (Regex.Match(sInput1, Regex.Escape(pattern), RegexOptions.Compiled | RegexOptions.IgnoreCase).Success)
            {
                throw new Exception("字符串中含有非法字符!");
            }
            else
            {
                output = output.Replace("'", "''");
            }
            return output;
        }
        #endregion

        #region
        /// <summary>
        /// 截取字符串函数
        /// </summary>
        /// <param name="Str">所要截取的字符串</param>
        /// <param name="Num">截取字符串的长度</param>
        /// <returns></returns>
        public static string GetSubString(string Str, int Num)
        {
            string titlemore = "20";
            if (Str == null || Str == "")
            {
                return "";
            }
            string outstr = string.Empty;
            int n = 0;
            foreach (char ch in Str)
            {
                n += System.Text.Encoding.Default.GetByteCount(ch.ToString());

                if (n > Num)
                {
                    break;
                }
                else
                {
                    outstr += ch;
                }
            }
            if (titlemore == "1")
            {
                if (Str.Length > Num)
                {
                    outstr += "...";
                }
            }
            return outstr;
        }

        /// <summary>
        /// 截取字符串函数
        /// </summary>
        /// <param name="Str">所要截取的字符串</param>
        /// <param name="Num">截取字符串的长度</param>
        /// <param name="Num">截取字符串后省略部分的字符串</param>
        /// <returns></returns>
        public static string GetSubString(string Str, int Num, string LastStr)
        {
            return (Str.Length > Num) ? Str.Substring(0, Num) + LastStr : Str;
        }
        #endregion

        /// <summary>
        /// 将字符串转成JSON规范的字符串
        /// </summary>
        /// <param name="s">要转换的字符串</param>
        /// <returns>带双引号的字符串</returns>
        public static String string2Json(String s)
        {
            StringBuilder sb = new StringBuilder(s.Length + 20);
            sb.Append('\"');
            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];
                switch (c)
                {
                    case '\"':
                        sb.Append("\\\"");
                        break;
                    case '\\':
                        sb.Append("\\\\");
                        break;
                    case '/':
                        sb.Append("\\/");
                        break;
                    case '\b':
                        sb.Append("\\b");
                        break;
                    case '\f':
                        sb.Append("\\f");
                        break;
                    case '\n':
                        sb.Append("\\n");
                        break;
                    case '\r':
                        sb.Append("\\r");
                        break;
                    case '\t':
                        sb.Append("\\t");
                        break;
                    default:
                        sb.Append(c);
                        break;
                }
            }
            sb.Append('\"');
            return sb.ToString();
        }
        /// <summary>
        /// 给字符串加上单引号
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static String AddDYH(object value)
        {
            if (null != value && !string.IsNullOrEmpty(value.ToString()))
            {
                return string.Format("'{0}'", value);
            }
                    
            return "''";
        }
    }
}
